// pages/settings/inbound/enter/enter.js
const util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: "2016-09-01",
    name: "",
    commodityid: "",
    unit: "",
    total: "",
    bag: "0",
    box: "",
    buyer: "",
    buyerid: "",
    sellprice: ""
  },

  bindDateChange: function(e) {
    console.log(e);
    this.setData({
      date: e.detail.value
    })
  },

  showTopTips: function() {
    var that = this;
    this.setData({
      showTopTips: true
    });
    setTimeout(function() {
      that.setData({
        showTopTips: false
      });
    }, 1500);
  },

  formSubmit: function(e) {
    var that = this

    if (!e.detail.value.box || (e.detail.value.box < 0 && e.detail.value.bag < 0) || !e.detail.value.bag || !e.detail.value.sellprice) {
      this.showTopTips();
      return; //验证提交是否为空
    }
    wx.request({
      url: 'http://192.168.3.2/cloudwarehouse/outbound/nullcheck.php',
      data: {
        id: that.data.commodityid, //不用Promise方法进行异步回调
        bag: that.data.bag,
        box: that.data.box,
      },
      success: function(res) {
        console.log(res.data)
        if (res.data == -1) {
          wx.showModal({
            title: '输入错误',
            content: '没有足够的库存了！',
            confirmText: '确定',
            showCancel: false
          })
        } else {
          wx.request({
            url: 'http://192.168.3.2/cloudwarehouse/outbound/out.php',
            data: {
              commodityid: that.data.commodityid,
              bag: that.data.bag,
              box: that.data.box,
              name: that.data.name,
              buyer: that.data.buyer,
              buyerid: that.data.buyerid,
              sellprice: that.data.sellprice,
              total: that.data.total,
              date: that.data.date,
              subdate: util.getNowDate(),
              openid: util.getOpenid()
            },

            success: function(res) {
              console.log(res.data)
              that.showSuccess()
            }
          })
        }
      }


    })
  },


  // nullcheck : function (){
  //   var that = this
  //   return new Promise(function (resolve, reject) {
  //   wx.request({
  //     url: 'http://192.168.3.2/cloudwarehouse/outbound/nullcheck.php',
  //     data:{
  //       id : that.data.commodityid,                                              //可以用Promise方法进行异步回调
  //       bag : that.data.bag,
  //       box : that.data.box,
  //     },
  //     success: res => resolve("TEST")
  //   })
  //   })
  // },

  showSuccess: function(e) {
    var key = 1
    wx.navigateTo({
      url: '../outbound/outsuccessfully/outsuccessfully',
    })
  },

  endBox: function(e) {
    wx.showLoading({
      title: '加载中',
    })
    this.setData({
      box: Number(e.detail.value),
    })
    this.setData({
      total: Number((this.data.box * this.data.sellprice + this.data.bag / this.data.unit * this.data.sellprice).toFixed(2))
    })
    wx.hideLoading()
  },

  endBag: function(e) {
    wx.showLoading({
      title: '加载中',
    })
    this.setData({
      bag: Number(e.detail.value)
    })
    this.setData({
      total: Number((this.data.box * this.data.sellprice + this.data.bag / this.data.unit * this.data.sellprice).toFixed(2))
    })
    wx.hideLoading()
  },

  endTotal: function(e) {
    this.setData({
      sellprice: Number(e.detail.value)
    })
    this.setData({

      total: Number((this.data.box * this.data.sellprice + this.data.bag / this.data.unit * this.data.sellprice).toFixed(2))
    })
  },


  /**
   * 
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      date: util.getNowFormatDate()
    })
    console.log(util.getNowDate())
  },




  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this
    wx.getStorage({
      key: 'selectedCommodity',
      success: function(res) {
        that.setData({
          name: res.data
        })
      },
    })

    wx.getStorage({
      key: 'selectedId',
      success: function(res) {
        that.setData({
          commodityid: res.data,
        })
      },
    })

    wx.getStorage({
      key: 'selectedUnit',
      success: function(res) {
        that.setData({
          unit: res.data
        })
      },
    })

    wx.getStorage({
      key: 'selectedBuyerId',
      success: function(res) {
        that.setData({
          buyerid: res.data,
        })
        wx.request({
          url: 'http://192.168.3.2/cloudwarehouse/outbound/getprice.php', //必须写在这，写在下面的话会不同步
          data: {
            buyerid: that.data.buyerid,
            commodityid: that.data.commodityid
          },
          success(res) {
            console.log(res)
            if (res.data[0]) {
              that.setData({
                sellprice: res.data[0][0]
              })
            } else {
              that.setData({
                sellprice: ""
              })
            }
          }
        })
      },
    })

    wx.getStorage({
      key: 'selectedBuyer',
      success: function(res) {
        that.setData({
          buyer: res.data
        })
      },
    })


  },


})